About three months ago I finally gained access to the internet and started browsing around Aminet. I usually look for utilities and source code to help me in my programming efforts on the Amiga. I also read Amiga Report and lo and behold I spotted a copy of the first Issue of Amiga Report Tech Journal. After reading the first and now second edition I thought the concept had great potential.
I know how difficult it is learning to program the Amiga especially as most Amiga magazines only touch the surface of programming, running the same old simple programming tutorials. I used to buy those magazines month in month out hoping to find something useful but no. So I soldiered on as most people have and learned what I know very slowly. I am by no means a great Amiga programmer and alot of people may already know most of the concepts that I will be talking about in the coming issues so if you think you can do better put together an Article and maybe you can teach me something!!
Ive been doing small development on the Amiga on and off for about 3 years mainly just to teach myself C and soon C++. I program Client Server Windows Application for a living, can Program over 10 different programming languages and have been programming for over eight years. I may not be an Amiga expert but I hope I know a bit about programming.
Well thats the background, Ive only had a week to put this article together so Im just going to do a few programming tips, mainly a few tricks I have learn't about ANSI C. I hope to do more article for future issues with a bit more meat in them!
if( a = b )
mistake by
declaring a define statement as follows#define EQU ==
Then anywhere you would normally use
if( a == b )
use
if( a EQU b )
. Your C compiler
may already have this defined. I always include a header file
of mine called Kens.h with this define and a few other things
just to make sure.
int i = 0;
Each object defined in this way will occupy exactly twice the storage space. Also the compiler will reset the variable every time the function is called. This involves copying the variable contents from a safe location in memory into your working variable.
This not only wastes CPU time, it also makes for larger
memory overheads and larger stack usage. In most cases the
variable can be changed to be static if this is not the case
then try to use a global buffer shared between all routines,
which you can initialise yourself. Example:
Replace: void func( void ) { char ls_Msg = "This is
time consuming";...}
With: void func( void ) { static char ls_Msg = "Much
Better";...}
( ++li_A < li_B )
There is no saving when they are being used in the simple
for of li_A++
or li_B++
. The
reason for the benefit of
( ++li_A < li_B )
over
( li_A++ <= li_B )
is that
the compiler must generate an extra jump instruction to
prevent the increment from happening.
if( li_A < li_B ) li_Z = 10; else li_Z = 0;
li_Z = 0; if( li_A < li_B ) li_Z = 10;
When you declare the register variables, declare them
singularly with the inner most variable declared first. Thus
making sure the most used variable is the register variable
whenever a spare register is available. Example:
Replace: register int li_I, li_J;
With: register int li_J; &47;* J in inner most
variable *&47;
register int li_I;
char __stdiowin[]="CON:0/0/700/550/Window Title";
This command works the same as the DOS shell program so you can adjust the size and coordinates of the window.
#define ABORT (void)printf("Aborting\n");exit(1);
It is easy then to use ABORT in other parts of the program
but it can cause problems when not used in a straight
forward manner such as in an if statement. In order to avoid
problems like this you should always enclose multi statement
macros in a do {} while(0) construct, for example:
#define ABORT do {(void)printf("Aborting\n");exit(1);}
while(0);
#define SQR( number ) ( number * number )
li_Size = SQR( 5 )
li_Size = ( 5 * 5 )
li_Size = SQR( 5 + 6 )
br>
equates toli_Size = ( 5 + 6 * 5 + 6 )
Therefore if we use the rule of always putting parentheses
around parameters we overcome the problem, for example:
#define SQR( number ) ( ( number ) * ( number ) )
li_Size = SQR( 5 + 6 )
gives
li_Size = ( ( 5 + 6 ) * ( 5 + 6 ) )
(NOTE) never use ++ and -- with macros, consider:
li_Size = SQR( li_Val++ )
equates to
li_Size = ( ( li_Val++ ) * ( li_Val++ ) )
Yuk!
Well I have to disagree, working in computing I am fortunate to have access to lots of books on programming and if there is a book I want to read I can order it free of charge. Most people recommend the K&R book as they have not tried many other books.
I found the K&R book very tuff going and Iam not new to programming. Therefore I would like to take this opportunity to recommend the best two book out of the 12 I have at my disposal:
As a quick reference: C Quick Reference, by QUE, ISBN 0-88022-372-3 ( about $8 ) Is a compact (154 pages) reference to the C language and is great for looking up commands and C syntax.
As a tutorial book: Using C, by QUE, ISBN 0-88022-571-8 ( about $30 ) Is almost 1000 pages. It is split into 3 books, 1) the tutorial is brilliant with loads of meaning full examples it start easy and will take you through most of the advanced stuff. 2) A complete C library reference. 3) An introduction to C++.
Well thats all for this issue I hope to have a full and hopefully useful routine to pick over next issue.
Ken Howe can be reached at khowe90@entergy.com